<?php

use kartik\base\AnimateAsset;
use kartik\growl\GrowlAsset;
use kartik\widgets\Growl;
use kzo\base\models\statics\OperationResult;
use kzo\base\widgets\ui\common\grid\GridView;
use yii\helpers\Html;
use yii\helpers\Url;
use kzo\base\models\statics\EntityModelStatus;
use kzo\base\models\statics\OperationEvent;
use kzo\base\widgets\ui\common\grid\ActionColumn;

/* @var $this yii\web\View */
/* @var $searchModel common\models\c2\search\WarehouseNoteSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
/* @var $model common\models\c2\search\WarehouseNoteSearch */

$this->title = Yii::t('app.c2', 'Warehouse Notes');
$this->params['breadcrumbs'][] = $this->title;
?>
<?php
echo Html::beginTag('div', ['style' => 'background:#ffeeba;padding:5px;', 'class' => 'mb-2']);
echo $this->render('/default/_detail.php', ['model' => $model]);
echo Html::endTag('div');
?>
<div class="warehouse-note-index">

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <?php echo GridView::widget([
        'dataProvider' => $dataProvider,
        // 'filterModel' => $searchModel,
        'id' => $model->getPrefixName('grid'),
        'pjax' => true,
        'hover' => true,
        'showPageSummary' => true,
        'panel' => ['type' => GridView::TYPE_PRIMARY, 'heading' => Yii::t('app.c2', 'Output Record')],
        'toolbar' => [
            [
                'content' =>
                    Html::a('<i class="fas fa-plus"></i>', ['edit'], [
                        'class' => 'btn btn-success',
                        'title' => Yii::t('app.c2', 'Add'),
                        'data-pjax' => '0',
                    ]) . ' ' .
                    Html::a(Yii::t('app.c2', 'Onekey output'), [
                        '/delivery-note/output/onekey-output',
                        'id' => $model->id,
                    ], [
                        'class' => 'btn btn-danger',
                        'id' => 'onekey-output',
                        'title' => Yii::t('app.c2', 'Onekey output'),
                        'data-pjax' => '0',
                    ]) . ' ' .
                    Html::a('<i class="fas fa-redo"></i>', Url::current(), [
                        'class' => 'btn btn-outline-secondary',
                        'id' => 'grid-refresh',
                        'title' => Yii::t('app.c2', 'Reset Grid')
                    ]),
                'options' => ['class' => 'btn-group mr-2']
            ],
            // '{export}',
            // '{toggleData}',
        ],
        'exportContainer' => ['class' => 'btn-group mr-2'],
        'exportConfig' => [],
        'columns' => [
            ['class' => 'kartik\grid\CheckboxColumn'],
            ['class' => 'kartik\grid\SerialColumn'],
            [
                'class' => 'kartik\grid\ExpandRowColumn',
                'value' => function ($model, $key, $index, $column) {
                    return GridView::ROW_COLLAPSED;
                },
                'detail' => function ($model, $key, $index, $column) {
                    return Yii::$app->controller->renderPartial('_detail', ['model' => $model]);
                },
            ],
            'id',
            // 'type',
            [
                'attribute' => 'type',
                'value' => function ($model) {
                    return \common\models\c2\statics\WarehouseNoteType::getLabel($model->type);
                }
            ],
            // 'ref_note_id',
            'label',
            // 'warehouse_id',
            // 'receiver_name',
            // 'updated_by',
            // 'created_by',
            'memo:ntext',
            // 'state',
            [
                'attribute' => 'state',
                'value' => function ($model) {
                    return \common\models\c2\statics\WarehouseNoteState::getLabel($model->state);
                }
            ],
            // 'status',
            // 'position',
            'created_at',
            // 'updated_at',
            [
                'attribute' => 'status',
                'class' => '\kartik\grid\EditableColumn',
                'editableOptions' => [
                    'inputType' => \kartik\editable\Editable::INPUT_DROPDOWN_LIST,
                    'formOptions' => ['action' => Url::toRoute('editColumn')],
                    'data' => EntityModelStatus::getHashMap('id', 'label'),
                    'displayValueConfig' => EntityModelStatus::getHashMap('id', 'label'),
                ],
                'filter' => EntityModelStatus::getHashMap('id', 'label'),
                'value' => function ($model) {
                    return $model->getStatusLabel();
                }
            ],
            [
                'class' => ActionColumn::className(),
                'template' => '{update} {delete} {commit}',
                'width' => '150px',
                'buttons' => [
                    'update' => function ($url, $model, $key) {
                        return Html::a('<span class="fas fa-pencil-alt"></span>', ['/delivery-note/output/edit', 'id' => $model->id], [
                            'title' => Yii::t('app.c2', 'Update'),
                            'data-pjax' => '0',
                            'class' => 'edit',
                        ]);
                    },
                    'commit' => function ($url, $model, $key) {
                        return Html::a('<span class="fas fa-sign-out-alt">' . Yii::t('app.c2', 'Commit') . '</span>', [
                            '/delivery-note/output/commit', 'id' => $model->id
                        ], [
                            'title' => Yii::t('app.c2', 'Commit'),
                            'data-pjax' => '0',
                            'class' => 'btn btn-primary btn-sm commit',
                        ]);
                    },
                    'delete' => function ($url, $model, $key) {
                        return Html::a('<span class="fas fa-trash"></span>', ['/delivery-note/output/delete', 'id' => $model->id], [
                            'title' => Yii::t('app.c2', 'Delete'),
                            'data-pjax' => '0',
                            'class' => 'delete',
                        ]);
                    },
                ]
            ],
        ],
    ]); ?>

</div>
<?php
\yii\bootstrap\Modal::begin([
    'id' => 'content-edit',
    'size' => 'modal-xl',
    'options' => [
        'tabindex' => false
    ],
]);

\yii\bootstrap\Modal::end();
// echo Growl::widget([
//     'type' => Growl::TYPE_SUCCESS,
//     'title' => 'Well done!',
//     'icon' => 'glyphicon glyphicon-ok-sign',
//     'body' => 'You successfully read this important alert message.',
//     'showSeparator' => true,
//     'delay' => 0,
//     'pluginOptions' => [
//         // 'showProgressbar' => true,
//         'placement' => [
//             'from' => 'top',
//             'align' => 'center',
//         ]
//     ]
// ]);
$setting = \yii\helpers\Json::encode([
    'type' => Growl::TYPE_SUCCESS,
    'title' => 'Well done!',
    'icon' => 'glyphicon glyphicon-ok-sign',
    'body' => 'You successfully read this important alert message.',
    'showSeparator' => true,
    'delay' => 0,
    'pluginOptions' => [
        // 'showProgressbar' => true,
        'placement' => [
            'from' => 'top',
            'align' => 'center',
        ]
    ]
]);
GrowlAsset::register($this);
AnimateAsset::register($this);
$js = "";

$js .= "jQuery(document).off('click', 'a.edit').on('click', 'a.edit', function(e) {
            e.preventDefault();
            jQuery('#content-edit').modal('show').find('.modal-content').html('" . Yii::t('app.c2', 'Loading...') . "').load(jQuery(e.currentTarget).attr('href'));
        });";

$js .= "jQuery(document).off('click', 'a.commit').on('click', 'a.commit', function(e) {
                e.preventDefault();
                var lib = window['krajeeDialog'];
                var url = jQuery(e.currentTarget).attr('href');
                lib.confirm('" . Yii::t('app.c2', 'Are you sure?') . "', function (result) {
                    if (!result) {
                        return;
                    }
                    jQuery.ajax({
                            url: url,
                            success: function(data) {
                                var lifetime = 6500;
                                if(data.meta.result == '" . kzo\base\models\statics\OperationResult::SUCCESS . "'){
                                    // jQuery('#{$model->getPrefixName('grid')}').trigger('" . \kzo\base\models\statics\OperationEvent::REFRESH . "');
                                    jQuery('#grid-refresh').click();
                                } else {
                                  lifetime = 16500;
                                }
                                $.notify(" . $setting . ");
                            },
                            error :function(data){alert(data.meta.message);}
                    });
                });
            });";

$js .= "jQuery(document).off('click', '#onekey-output').on('click', '#onekey-output', function(e) {
                e.preventDefault();
                var lib = window['krajeeDialog'];
                var url = jQuery(e.currentTarget).attr('href');
                lib.confirm('" . Yii::t('app.c2', 'Are you sure?') . "', function (result) {
                    if (!result) {
                        return;
                    }
                    jQuery.ajax({
                            url: url,
                            success: function(data) {
                                var lifetime = 6500;
                                if(data.meta.result == '" . kzo\base\models\statics\OperationResult::SUCCESS . "'){
                                    // jQuery('#{$model->getPrefixName('grid')}').trigger('" . \kzo\base\models\statics\OperationEvent::REFRESH . "');
                                    jQuery('#grid-refresh').click();
                                } else {
                                  lifetime = 16500;
                                }
                                $.notify(" . $setting . ");
                            },
                            error :function(data){alert(data.meta.message);}
                    });
                });
            });";

$js .= "jQuery(document).off('click', 'a.commit').on('click', 'a.commit', function(e) {
                e.preventDefault();
                var lib = window['krajeeDialog'];
                var url = jQuery(e.currentTarget).attr('href');
                lib.confirm('" . Yii::t('app.c2', 'Are you sure?') . "', function (result) {
                    if (!result) {
                        return;
                    }
                    jQuery.ajax({
                            url: url,
                            success: function(data) {
                                var lifetime = 6500;
                                if(data.meta.result == '" . kzo\base\models\statics\OperationResult::SUCCESS . "'){
                                    // jQuery('#{$model->getPrefixName('grid')}').trigger('" . \kzo\base\models\statics\OperationEvent::REFRESH . "');
                                    jQuery('#grid-refresh').click();
                                } else {
                                  lifetime = 16500;
                                }
                                $.notify(" . $setting . ");
                            },
                            error :function(data){alert(data.meta.message);}
                    });
                });
            });";

$js .= "jQuery(document).off('click', 'a.delete').on('click', 'a.delete', function(e) {
                e.preventDefault();
                var lib = window['krajeeDialog'];
                var url = jQuery(e.currentTarget).attr('href');
                lib.confirm('" . Yii::t('app.c2', 'Are you sure?') . "', function (result) {
                    if (!result) {
                        return;
                    }
                    jQuery.ajax({
                            url: url,
                            success: function(data) {
                                var lifetime = 6500;
                                if(data.meta.result == '" . kzo\base\models\statics\OperationResult::SUCCESS . "'){
                                    // jQuery('#{$model->getPrefixName('grid')}').trigger('" . \kzo\base\models\statics\OperationEvent::REFRESH . "');
                                    jQuery('#grid-refresh').click();
                                } else {
                                  lifetime = 16500;
                                }
                                $.notify(" . $setting . ");
                            },
                            error :function(data){alert(data.meta.message);}
                    });
                });
            });";

$this->registerJs($js);

?>
